package com.qf.springmybatis.service.impl;

import com.qf.springmybatis.dao.UserDao;
import com.qf.springmybatis.entity.User;
import com.qf.springmybatis.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.sql.SQLException;

@Service("userService")
@Transactional
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao userDao;

    public UserDao getUserDao() {
        return userDao;
    }

    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }

    @Transactional(propagation= Propagation.SUPPORTS)
    @Override
    public User getUser(Integer id) {
        System.out.println("UserServiceImpl 查询id:" + id);
        return userDao.getUserById(id);
    }

    @Transactional(rollbackFor=Exception.class)
    @Override
    public Integer addUser(User user) throws SQLException {
        User userr = userDao.getUserById(16);
        userr.setId(38);
        userr.setName("三十九");
        userDao.insertUser(userr);
        if (true){
            throw new SQLException();
        }
        userr.setId(39);
        userr.setName("四十");
        userDao.insertUser(userr);
        return 2;
    }
}
